package com.example.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.himalaya.R;
import com.example.himalaya.views.CircleImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayerTrackPagerAdapter extends PagerAdapter {
    private List<Track> mData = new ArrayList<>();
    private View mItemView;
    private CircleImageView mItem;

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        mItemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track_page,container,false);
        container.addView(mItemView);
        rotateAnim();
        //设置数据
        //找到控件
        mItem = mItemView.findViewById(R.id.track_pager_item);
       //设置图片
       String coverUrlLarge= mData.get(position).getCoverUrlLarge();
        Picasso.with(container.getContext()).load(coverUrlLarge).into(mItem, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {

            }
        });
        return mItemView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    public void setData(List<Track> list) {
        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();
    }

    public void rotateAnim() {
        Animation anim =new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(20000); // 设置动画时间
        anim.setRepeatCount(-1);
        anim.setRepeatMode(Animation.RESTART);

        anim.setInterpolator(new LinearInterpolator()); // 设置插入器
        anim.setFillAfter(false);// 设置旋转后停止
        mItemView.startAnimation(anim);
    }

}
